package com.java.test.util;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Base64;

/**
 * TODO
 *
 * @author lijie1
 * @date 2020/7/9 0009
 */
@Slf4j
@Data
public class RequestGameServiceUtil {
    private static String backServerKey = "h4w6twlq88_9&3ljhn9K0mb;nh43*3szA_l0sh";

    private static String hmacHash1Key = "A3oX9lyu";

    public static JSONObject getService(String url){
        try {
            URL gitUrl = new URL(url);
            URLConnection urlConnection = gitUrl.openConnection();
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(10000);
            urlConnection.connect();
            InputStream inputStream = urlConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String resMsgString = bufferedReader.readLine();
            JSONObject resMsg = new JSONObject(resMsgString);
            log.info(resMsgString);
            return resMsg;
        } catch (Exception e) {
          System.out.println(e.getLocalizedMessage());
            return null;
        }
    }

    /**
     * 获取完整的url
     * @author lijie1
     * @date 2020/7/9 0009
     */
    public static String createUrl(String time,String msgId,String data,String offLineUserUrl){
        StringBuffer url = new StringBuffer(offLineUserUrl);
        try {
            url.append("time=").append(time).append("&");
            url.append("msgId=").append(msgId).append("&");
            url.append("data=").append(URLEncoder.encode(data,"utf-8")).append("&");
            url.append("sign=").append(createSign(time,msgId,data,backServerKey,hmacHash1Key));
        }catch (Exception e){
            e.printStackTrace();
        }
        return url.toString();
    }

    /**
     * 封装参数
     * http://192.168.1.13:7070/backServerGameWorker/?
     * @param time 时间戳
     * @param msgId 消息code
     * @param data 消息内容
     * @param BackServerKey h4w6twlq88_9&3ljhn9K0mb;nh43*3szA_l0sh
     * @param HmacHash1Key A3oX9lyu
     * @author lijie1
     * @date 2020/7/9 0009
     */
    private static String createSign (String time,String msgId,String data,String BackServerKey,String HmacHash1Key) throws Exception{

        String conStr=(Mmd5.createString(data)+time+msgId).toUpperCase()+BackServerKey;
        String varMap="{\"A\":\"2s\",\"a\":\"3\",\"B\":\"e3\",\"b\":\"2\",\"C\":\"2l\",\"c\":\"0\",\"D\":\"65\",\"d\":\"h5\",\"E\":\"29\",\"e\":\"5\",\"F\":\"6\",\"f\":\"p0\",\"G\":\"6\",\"g\":\"g\",\"H\":\"@\",\"h\":\"ml\",\"I\":\"n6p\",\"i\":\"4\",\"J\":\"4\",\"j\":\"3\",\"K\":\"ng\",\"k\":\"4\",\"L\":\"2bn\",\"l\":\"c\",\"M\":\"s\",\"m\":\"5\",\"N\":\"9\",\"n\":\"64\",\"O\":\"9\",\"o\":\"7\",\"P\":\"2\",\"p\":\"gh\",\"Q\":\"6\",\"q\":\"76\",\"R\":\"_\",\"r\":\"5\",\"S\":\"5\",\"s\":\"2\",\"T\":\"7\",\"t\":\"6\",\"U\":\"8l\",\"u\":\"hg\",\"V\":\".\",\"v\":\"gh\",\"W\":\"2\",\"w\":\"j\",\"X\":\"n\",\"x\":\"h\",\"Y\":\"62\",\"y\":\"2h\",\"Z\":\"n\",\"z\":\"a\",\"1\":\"c\",\"2\":\"n\",\"3\":\"i\",\"4\":\"sb\",\"5\":\"x\",\"6\":\"p\",\"7\":\"3\",\"8\":\"m\",\"9\":\"r\"}";
        log.debug(conStr);
        JSONObject varMapJson=new JSONObject(varMap);
        ArrayList<String> cc=new ArrayList<>();
        for (int ii=0;ii<conStr.length();ii++){
            cc.add(String.valueOf(conStr.charAt(ii)));
        }
        for (String  vark:cc){
            if(varMapJson.has(vark)) {
                conStr = conStr.replaceAll(vark, varMapJson.getString(vark));
            }
        }
        log.debug(conStr);
        byte[] conStrBytes=conStr.getBytes("UTF-8");
        byte[] keyBytes=HmacHash1Key.getBytes("UTF-8");
        SecretKey secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        // 用给定密钥初始化 Mac 对象
        mac.init(secretKey);
        byte[] body =mac.doFinal(conStrBytes);
        String signAllStr=new String(Base64.getEncoder().encode(body));
        return  signAllStr.replaceAll("[+]","");
    }
}
